package com.jgit;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * Commit对象：存储提交信息
 */
public class Commit extends GitObject {
    private String treeHash;      // 指向的tree对象
    private List<String> parents; // 父提交的哈希列表
    private String author;         // 作者信息
    private String committer;      // 提交者信息
    private String message;       // 提交消息
    private long timestamp;        // 时间戳
    
    public Commit() {
        this.parents = new ArrayList<>();
    }
    
    public static Commit fromContent(byte[] content) {
        Commit commit = new Commit();
        String contentStr = new String(content, StandardCharsets.UTF_8);
        String[] lines = contentStr.split("\n");
        
        for (String line : lines) {
            if (line.startsWith("tree ")) {
                commit.treeHash = line.substring(5);
            } else if (line.startsWith("parent ")) {
                commit.parents.add(line.substring(7));
            } else if (line.startsWith("author ")) {
                commit.author = line.substring(7);
                // 解析时间戳
                String[] parts = commit.author.split(" ");
                if (parts.length > 0) {
                    try {
                        commit.timestamp = Long.parseLong(parts[parts.length - 1]);
                    } catch (NumberFormatException e) {
                        // 忽略
                    }
                }
            } else if (line.startsWith("committer ")) {
                commit.committer = line.substring(10);
            } else if (line.isEmpty()) {
                // 空行后是提交消息
                int messageStart = contentStr.indexOf("\n\n");
                if (messageStart != -1) {
                    commit.message = contentStr.substring(messageStart + 2);
                }
                break;
            }
        }
        
        return commit;
    }
    
    @Override
    public String getType() {
        return "commit";
    }
    
    @Override
    public byte[] getRawContent() throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        baos.write(("tree " + treeHash + "\n").getBytes(StandardCharsets.UTF_8));
        
        for (String parent : parents) {
            baos.write(("parent " + parent + "\n").getBytes(StandardCharsets.UTF_8));
        }
        
        if (author != null) {
            baos.write(("author " + author + "\n").getBytes(StandardCharsets.UTF_8));
        }
        
        if (committer != null) {
            baos.write(("committer " + committer + "\n").getBytes(StandardCharsets.UTF_8));
        }
        
        baos.write("\n".getBytes(StandardCharsets.UTF_8));
        
        if (message != null) {
            baos.write(message.getBytes(StandardCharsets.UTF_8));
        }
        
        return baos.toByteArray();
    }
    
    public String getTreeHash() {
        return treeHash;
    }
    
    public void setTreeHash(String treeHash) {
        this.treeHash = treeHash;
    }
    
    public List<String> getParents() {
        return parents;
    }
    
    public void addParent(String parentHash) {
        parents.add(parentHash);
    }
    
    public String getAuthor() {
        return author;
    }
    
    public void setAuthor(String author) {
        this.author = author;
    }
    
    public String getCommitter() {
        return committer;
    }
    
    public void setCommitter(String committer) {
        this.committer = committer;
    }
    
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    public long getTimestamp() {
        return timestamp;
    }
    
    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }
}

